package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T18;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT06Service;
import com.ruoyi.service.IT18Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.T23;
import com.ruoyi.service.IT23Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统公告Controller
 *
 * @author ruoyi
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/system/T23")
@Api(tags = "系统公告管理")
public class T23Controller extends BaseController
{
    @Autowired
    private IT23Service t23Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT18Service t18Service;

    @Autowired
    private IT06Service t06Service;

    /**
     * 查询系统公告列表
     */

    @GetMapping("/list")
    @ApiOperation(value = "查询系统公告列表")
    public TableDataInfo list(T23 t23,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        Long ComId = null;
        if(ObjectUtils.isEmpty(t18)){
            T06 t06 = t06Service.selectT06ByF0601(userId);
            if(!ObjectUtils.isEmpty(t06)){
                ComId = t06.getF0301();
            }
        }else{
            ComId = t18.getF0301();
        }
        t23.setF0301(ComId);
        t23.setF2309(0L);
        List<T23> list = t23Service.selectT23List(t23);
        return getDataTable(list);
    }

    /**
     * 导出系统公告列表
     */

    @Log(title = "系统公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出系统公告列表")
    public void export(HttpServletResponse response, T23 t23)
    {
        List<T23> list = t23Service.selectT23ListNoPage(t23);
        ExcelUtil<T23> util = new ExcelUtil<T23>(T23.class);
        util.exportExcel(response, list, "系统公告数据");
    }

    /**
     * 获取系统公告详细信息
     */

    @GetMapping(value = "/{F2301}")
    @ApiOperation(value = "获取系统公告详细信息")
    public AjaxResult getInfo(@PathVariable("F2301") Long F2301)
    {
        return success(t23Service.selectT23ByF2301(F2301));
    }

    /**
     * 新增系统公告
     */

    @Log(title = "系统公告", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增系统公告")
    @PostMapping
    public AjaxResult add(@RequestBody T23 t23, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        Long ComId = null;
        if(ObjectUtils.isEmpty(t18)){
            t23.setF2708("运营公司");
            t23.setF2307("运营人员");
        }else{
            t23.setF1801(t18.getF1801());
            t23.setF0301(t18.getF0301());
            t23.setF2307(t18.getF1804());
            t23.setF2708(t18.getF1808());
        }
        t23.setF2305(new Date());
        t23.setF2309(0L);
        return toAjax(t23Service.insertT23(t23));
    }



    /**
     * 修改系统公告
     */

    @Log(title = "系统公告", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改系统公告")
    @PutMapping
    public AjaxResult edit(@RequestBody T23 t23)
    {
        return toAjax(t23Service.updateT23(t23));
    }

    /**
     * 删除系统公告
     */

    @Log(title = "系统公告", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除系统公告")
	@DeleteMapping("/{F2301s}")
    public AjaxResult remove(@PathVariable Long[] F2301s)
    {
        return toAjax(t23Service.deleteT23ByF2301s(F2301s));
    }


    @GetMapping("/getWxNotice")
    public AjaxResult getWxNotice(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T06 t06 = t06Service.selectT06ByF0601(userId);
        Long F0301 = t06.getF0301();
        T23 t23 = new T23();
        t23.setF0301(F0301);
        t23.setF2306(0L);
        List<T23> list = t23Service.selectT23ListNoPage(t23);
        List<T23> realList = new ArrayList<>();
        for(T23 t : list){
            if(t.getF2309()==1L){
                if(Objects.equals(t.getF0601(), t06.getF0601())){
                    realList.add(t);
                }
            }else{
                realList.add(t);
            }
        }
        return AjaxResult.success(list);
    }

}
